<template>
  <div></div>
</template>
<script>
import { setToken, navToAuthor, setIdToken } from "@/utils/auth";
import { getOrganization } from "@/api/organization";
export default {
  data() {
    return {
      access_token: null,
      id_token: null,
    };
  },
  methods: {
    handleLogin() {
      this.$route.push({ name: "Login" });
    },
    resolveToken(value, name) {
      if (!value) return null;
      let keyvalues = value.substring(1).split("&");
      let result = "";
      for (let i = 0; i < keyvalues.length; i++) {
        let spilts = keyvalues[i].split("=");

        if (spilts.length < 2) continue;

        let key = spilts[0];
        let value = spilts[1];

        if (key === name) {
          result = value;
          break;
        }
      }
      return result;
    },
    getOrganization(callback) {
      getOrganization().then((res) => {
        this.$store.commit("organization/SET_ORG", res);
        if (callback) callback();
      });
    },
  },
  mounted() {
    this.access_token = this.resolveToken(this.$route.hash, "access_token");
    this.id_token = this.resolveToken(this.$route.hash, "id_token");
    if (this.access_token) {
      setToken(this.access_token);
      if (this.id_token) setIdToken(this.id_token);
      this.$store.commit("user/SET_TOKEN", this.access_token);
      this.$router.push({ path: "/" });
      // this.getOrganization(() => {
      //   this.$router.push({ path: "/" });
      // });
    } else {
      navToAuthor();
    }
  },
};
</script>
